package cn.sdc.bugKiller.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.sdc.bugKiller.bean.BugBean;
import cn.sdc.bugKiller.bean.BugSearchBean;
import cn.sdc.bugKiller.bean.BugStatus;
import cn.sdc.bugKiller.bean.MemberBean;
import cn.sdc.bugKiller.bean.PriorityBean;
import cn.sdc.bugKiller.service.BugDealService;
import cn.sdc.bugKiller.service.CodeDealService;
import cn.sdc.bugKiller.service.ContextService;

public class SearchBugServlet extends BasicServlet {
	
	public static String SEARCH_BEAN = "searchBean";
	public static String METHOD_UNFIX = "unfix";
	public static String METHOD_APPROVAL = "approval";
	public static String METHOD_CLOSED = "closed";
	public static String METHOD_URGENTLY = "urgently";
	public static String METHOD_MYASSIGNED = "assignToMe";
	public static String METHOD_MYCREATED = "myCreated";
	
	private static BugDealService service = new BugDealService();
	
	public void exceute(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		setMenu(MENU_ALLBUG);
		setBeanName(SEARCH_BEAN);
		
		if(METHOD_UNFIX.equals(methodName)){
			unfixBugSearch(req,resp);
		}else if(METHOD_APPROVAL.equals(methodName)){
			approvalBugSearch(req,resp);
		}else if(METHOD_CLOSED.equals(methodName)){
			closedBugSearch(req,resp);
		}else if(METHOD_MYASSIGNED.equals(methodName)){
			assignToMeBugSearch(req,resp);
		}else if(METHOD_MYCREATED.equals(methodName)){
			myCreatedBugSearch(req,resp);
		}else{
			BugSearchBean searchBean = (BugSearchBean) getBean(req,BugSearchBean.class);
			List<BugBean> beanList = service.searchBugList(searchBean);
			setBeanAndForward(searchBean, beanList,req, resp, URI_ALLBUG_JSP);
		}
	}
	
	public void unfixBugSearch(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			BugSearchBean searchBean = new BugSearchBean();
			searchBean.setStatus(BugStatus.STATUS_UNFIX);
			List<BugBean> beanList = service.searchUnfixBugList();
			setBeanAndForward(searchBean, beanList,req, resp, URI_ALLBUG_JSP);
	}
	
	public void approvalBugSearch(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		BugSearchBean searchBean = new BugSearchBean();
		searchBean.setStatus(BugStatus.STATUS_APPROVAL);
		List<BugBean> beanList = service.searchApprovalBugList();
		setBeanAndForward(searchBean, beanList,req, resp, URI_ALLBUG_JSP);
	}
	
	public void closedBugSearch(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		BugSearchBean searchBean = new BugSearchBean();
		searchBean.setStatus(BugStatus.STATUS_CLOSED);
		List<BugBean> beanList = service.searchClosedBugList();
		setBeanAndForward(searchBean, beanList,req, resp, URI_ALLBUG_JSP);
	}
	public void assignToMeBugSearch(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		setMenu(MENU_MYBUG);
		
		BugSearchBean searchBean = (BugSearchBean) getBean(req,BugSearchBean.class);
		if(ContextService.getCurrentUser() == null){
			searchBean.setMyCreatedBugList(null);
		}else{
			searchBean.setHandler(ContextService.getCurrentUser().getId());
			searchBean.setStatus(BugStatus.STATUS_UNFIX);
			searchBean.setMyUnfixBugList(service.searchBugList(searchBean)); 
			searchBean.setStatus(BugStatus.STATUS_APPROVAL);
			List<BugBean> myFixedBugList = service.searchBugList(searchBean);
			searchBean.setStatus(BugStatus.STATUS_CLOSED);
			List<BugBean> myClosedBugList = service.searchBugList(searchBean);
			
			if(myFixedBugList == null){
				myFixedBugList = myClosedBugList;
			}else if(myClosedBugList != null){
				myFixedBugList.addAll(myClosedBugList);
			}
			searchBean.setMyClosedBugList(myFixedBugList); 
		}
		List<BugBean> beanList = service.searchClosedBugList();
		setBeanAndForward(searchBean, beanList,req, resp, URI_MYBUG_JSP);
	}
	public void myCreatedBugSearch(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		setMenu(MENU_MYCREATEDBUG);
		
		BugSearchBean searchBean = (BugSearchBean) getBean(req,BugSearchBean.class);
		if(ContextService.getCurrentUser() == null){
			searchBean.setMyCreatedBugList(null);
		}else{
			searchBean.setAssigner(ContextService.getCurrentUser().getId());
			searchBean.setMyCreatedBugList(service.searchBugList(searchBean));
		}
		setBeanAndForward(searchBean, null,req, resp, URI_MYCREATEDBUG_JSP);
	}
	
	private void setBeanAndForward(BugSearchBean searchBean,List<BugBean> beanList,HttpServletRequest req, HttpServletResponse resp, String frowardUri)
			throws ServletException, IOException {
		searchBean.setBugList(beanList);
		req.setAttribute(SEARCH_BEAN, searchBean);
		DropdownMaps.setDropdownMap(req);
		forward(req, resp, frowardUri);
	}
	
}
